import pygame
from config import config, uiconfig
from ui import events, src
from tools import *
from decimal import *
import pyautogui
import tkinter

x_pos_rev = 64
y_pos_rev = 0


def pos_to(pos):
    return (pos[0]-x_pos_rev, pos[1]-y_pos_rev)


def show_input_box(text, pos, x, y, refresh_func):
    root = tkinter.Tk()
    root.title("InputBox-ckpedit")
    # root.overrideredirect(True)
    root.geometry("{}x{}+{}+{}".format(pos[0], pos[1], x, y))
    text_var = tkinter.StringVar()
    text_var.set(text)
    inputs = tkinter.Entry(
        root, text=text, textvariable=text_var, borderwidth=0, highlightthickness=0)
    inputs.pack(fill=tkinter.BOTH)
    # root.attributes("-alpha", 1)
    inputs.focus_set()

    def refresh_screen(events):
        refresh_func(text_var.get())

    def close_window(events):
        root.destroy()
    inputs.bind("<Key-Return>", close_window)
    inputs.bind("<KeyRelease>", refresh_screen)
    root.bind("<FocusOut>", close_window)
    root.mainloop()
    return text_var.get()


class InputBox:
    text = ""

    def __init__(self, pos):
        self.pos = pos
        self.rect = pygame.Rect(pos)
        self.hover = 0
        self.screen = None

    def event(self, input_show):
        if (input_show == 0):
            if events.pressmouse == 1:
                if self.rect.collidepoint(pos_to(pygame.mouse.get_pos())):
                    # info("ui/weights/<InputBox>", "open input window")
                    self.hover = 1

                    def updates(text):
                        self.text = text
                        self.update(self.screen, 1)

                    mouse_pos = pyautogui.position()
                    window_pos = (
                        mouse_pos[0]-pygame.mouse.get_pos()[0], mouse_pos[1]-pygame.mouse.get_pos()[1])
                    self.text = show_input_box(self.text, (self.rect[2], self.rect[3]), window_pos[0]+self.rect[0]+x_pos_rev,
                                               window_pos[1]+self.rect[1]+y_pos_rev, updates)
                    self.hover = 0

    def update(self, screen, special_update=0):
        self.screen = screen
        weight_surface = pygame.Surface((self.rect[2], self.rect[3]))
        self.event(special_update)
        pygame.draw.rect(
            weight_surface, uiconfig.BACKGROUND_COLOR, (0, 0, self.pos[2], self.pos[3]), 0)
        if self.hover == 1:
            underline = "_"
            pygame.draw.rect(weight_surface, uiconfig.WEIGHT_HOVERCOLOR,
                             (0, self.pos[3]-2, self.pos[2], 2), 0)
        else:
            underline = ""
            pygame.draw.rect(weight_surface, uiconfig.WEIGHT_DCOLOR,
                             (0, self.pos[3]-2, self.pos[2], 2), 0)
        weight_surface.blit(src.basic_font_20.render(
            str(self.text)+underline, config.config.ANTIALIAS_FONT, uiconfig.FONT_COLOR), (2, 2))
        screen.blit(
            weight_surface, (self.rect[0], self.rect[1]))
        if (special_update == 1):
            pygame.display.update(
                (self.rect[0]+x_pos_rev, self.rect[1]+y_pos_rev, self.rect[2], self.rect[3]))

    def get(self):
        return self.text


class IntInputBox():
    text = 0

    def __init__(self, pos, max_lim=999999, min_lim=0):
        self.pos = pos
        self.rect = pygame.Rect(pos)
        self.hover = 0
        self.screen = None
        self.max_lim = max_lim
        self.min_lim = min_lim

    def event(self):
        if events.pressmouse == 1:
            if self.rect.collidepoint(pos_to(pygame.mouse.get_pos())):
                self.hover = 1
            else:
                self.hover = 0
        if self.hover and events.keypress != 0:
            if pygame.key.name(events.keypress) == 'return':
                self.hover = 0
            elif pygame.key.name(events.keypress) == 'backspace':
                self.text //= 10
            elif events.keypress <= 255 and str(chr(events.keypress)).isdigit():
                self.text = self.text*10+int(str(chr(events.keypress)))
            if (self.text > self.max_lim):
                self.text = self.max_lim
            elif (self.text < self.min_lim):
                self.text = self.min_lim

    def update(self, screen):
        self.screen = screen
        weight_surface = pygame.Surface((self.rect[2], self.rect[3]))
        self.event()
        pygame.draw.rect(
            weight_surface, uiconfig.BACKGROUND_COLOR, (0, 0, self.pos[2], self.pos[3]), 0)
        if self.hover == 1:
            underline = "_"
            pygame.draw.rect(weight_surface, uiconfig.WEIGHT_HOVERCOLOR,
                             (0, self.pos[3]-2, self.pos[2], 2), 0)
        else:
            underline = ""
            pygame.draw.rect(weight_surface, uiconfig.WEIGHT_DCOLOR,
                             (0, self.pos[3]-2, self.pos[2], 2), 0)
        weight_surface.blit(src.basic_font_20.render(
            str(self.text)+underline, config.config.ANTIALIAS_FONT, uiconfig.FONT_COLOR), (2, 2))
        screen.blit(
            weight_surface, (self.rect[0], self.rect[1]))

    def get(self):
        return self.text


class FloatInputBox(IntInputBox):
    text = Decimal(1)
    dotmode = 0

    def event(self):
        if events.pressmouse == 1:
            if self.rect.collidepoint(pos_to(pygame.mouse.get_pos())):
                self.hover = 1
            else:
                self.hover = 0
        if self.hover and events.keypress != 0:
            if pygame.key.name(events.keypress) == 'return':
                self.hover = 0
            elif pygame.key.name(events.keypress) == 'backspace':
                if self.dotmode == 0:
                    self.text //= 10
                elif self.dotmode > 1 and self.dotmode < 10:
                    self.dotmode -= 1
                    self.text = Decimal(str(self.text)[:-1])
                else:
                    self.dotmode = 0
            elif events.keypress <= 255 and str(chr(events.keypress)) == ".":
                if self.dotmode == 0:
                    self.dotmode = 1
                elif self.dotmode == 1:
                    self.dotmode = 0

            elif events.keypress <= 255 and str(chr(events.keypress)).isdigit():
                if self.dotmode == 0:
                    self.text = self.text*10+int(str(chr(events.keypress)))
                else:
                    self.text = self.text + \
                        (Decimal(1)/(10**self.dotmode)) * \
                        int(str(chr(events.keypress)))
                    self.dotmode += 1
            if (self.text > self.max_lim):
                self.text = self.max_lim
            elif (self.text < self.min_lim):
                self.text = self.min_lim

    def get(self):
        return float(self.text)

    def update(self, screen):
        pygame.draw.rect(screen, uiconfig.BACKGROUND_COLOR, self.pos, 0)
        self.event()
        if self.dotmode == 1:
            showfloat = "."
        else:
            showfloat = ""
        if self.hover == 1:
            underline = "_"
            pygame.draw.rect(screen, uiconfig.WEIGHT_HOVERCOLOR,
                             (self.pos[0], self.pos[1]+self.pos[3]-2, self.pos[2], 2), 0)
        else:
            underline = ""
            pygame.draw.rect(screen, uiconfig.WEIGHT_DCOLOR,
                             (self.pos[0], self.pos[1]+self.pos[3]-2, self.pos[2], 2), 0)
        screen.blit(src.basic_font_20.render(str(self.text)+showfloat+underline,
                    config.config.ANTIALIAS_FONT, uiconfig.FONT_COLOR), (self.pos[0]+2, self.pos[1]+2))


class Button():
    def __init__(self, pos, text, func):
        self.pos = pos
        self.rect = pygame.Rect(pos)
        self.text = text
        self.hover = 0
        self.func = func

    def event(self):
        if events.pressmouse == 1:
            if self.rect.collidepoint(pos_to(pygame.mouse.get_pos())):
                self.hover = 1
            else:
                self.hover = 0
        else:
            if self.rect.collidepoint(pos_to(pygame.mouse.get_pos())) and self.hover == 1:
                self.func()
                self.hover = 0
            else:
                self.hover = 0

    def update(self, screen):
        if (self.hover == 0):
            pygame.draw.rect(screen, uiconfig.WEIGHT_DCOLOR, self.pos, 1)
        else:
            pygame.draw.rect(screen, uiconfig.WEIGHT_HOVERCOLOR, self.pos, 0)
        screen.blit(src.basic_font_20.render(str(self.text), config.config.ANTIALIAS_FONT,
                    uiconfig.FONT_COLOR), (self.pos[0]+2, self.pos[1]+2))
        self.event()


class GlobalButton(Button):
    def __init__(self, icon, func, pos=(840, 504, 64, 64)):
        self.pos = pos
        self.rect = pygame.Rect(pos)
        self.icon = icon
        self.hover = 0
        self.func = func

    def event(self):
        def check_pos(mypos, mousepos):
            zx = abs(mypos[0]+mypos[2]/2-mousepos[0])
            zy = abs(mypos[1]+mypos[3]/2-mousepos[1])
            r = mypos[2]/2
            return (zx*zx+zy*zy) <= r*r  # sqrt(zx*zx+zy*zy)<=r
        if events.pressmouse == 1:
            if check_pos(self.pos, pos_to(pygame.mouse.get_pos())):
                self.hover = 1
            else:
                self.hover = 0
        else:
            if check_pos(self.pos, pos_to(pygame.mouse.get_pos())) and self.hover == 1:
                self.func()
                self.hover = 0
            else:
                self.hover = 0

    def update(self, screen):
        if (self.hover == 0):
            pygame.draw.circle(screen, uiconfig.WEIGHT_SHADECOLOR,
                               (self.pos[0]+self.pos[2]/2+2, self.pos[1]+self.pos[3]/2+2), self.pos[2]/2+2)
        pygame.draw.circle(screen, uiconfig.WEIGHT_HOVERCOLOR,
                           (self.pos[0]+self.pos[2]/2, self.pos[1]+self.pos[3]/2), self.pos[2]/2)
        screen.blit(self.icon, (self.pos[0], self.pos[1]))
        self.event()
